package com.buba.system.service.Impl;

import com.buba.system.dao.MenuMapper;
import com.buba.system.pojo.Menu;
import com.buba.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    private MenuMapper menuMapper;

    @Override
    public List<Menu> selMenu(Integer pid) {
        List<Menu> list = menuMapper.selMenu(pid);
        for(Menu menu : list){
            //获取菜单的id 进行查询该id下存不存在子菜单
            Integer count = menuMapper.selCount(menu.getId());
            //如果大于0 说明该菜单下存在子菜单
            if(count >0)
                //就把该父级下的子菜单set保存到children属性中
                //在方法的内部调用自身的方式叫做递归
                menu.setChildren(selMenu(menu.getId()));
        }
        return list;
    }


    public MenuMapper getMenuMapper() {
        return menuMapper;
    }
    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }
}
